var app = getApp(); //获取应用实例
const quote = app.globalData.quote;
const mta = quote.mta;
const api = quote.api;
const path = quote.paths;

Page({
    data: {
        path: path,
        spareMoney: '',
        grade: null,
        preferentialList: [],
    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        var that = this;
        mta.Page.init();

        that.GetAccountPresentationRulesList();
    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成
    },
    onShow: function() {
        // 生命周期函数--监听页面显示
    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏
    },
    onUnload: function() {
        // 生命周期函数--监听页面卸载
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function(params) {
        var that = this;

        that.GetAccountPresentationRulesList(null, function(params) {
            //数据加载完后停止刷新
            setTimeout(() => {
                console.log('停止刷新');
                wx.stopPullDownRefresh();
            }, 1000);
        })

    },
    onReachBottom: function() {
        // 页面上拉触底事件的处理函数
    },
    GetAccountPresentationRulesList: function(params, cb) {
        var that = this;

        var dataDic = {
            OpenId: app.globalData.openId
        }
        api.methodGetAccountPresentationRulesList({
            dataDic: dataDic,
            complete: function(res) {
                console.log('获取账户提现规则列表', res);
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.data.msg,
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            }
                        }
                    })
                    return
                }
                that.setData({
                    spareMoney: res.data.data.Balance,
                    preferentialList: res.data.data.AccountPresentationRulesList
                })
                typeof cb == 'function' && cb();
            }
        })
    },
    gotoShouldKnow: function(params) {
        var that = this;

        app.aldstat.sendEvent('账户-点击用户须知');
        mta.Event.stat("myAccount_clickshouldKnow", {});

        wx.navigateTo({
            url: '../shouldKnow/shouldKnow',
        })
    },
    getGrade: function(e) {
        var that = this;

        // console.log(e.currentTarget.dataset.grade)
        that.setData({
            grade: e.currentTarget.dataset.grade
        })
    },
    submit: function(e) {
        var that = this;

        console.log('提现');
        app.aldstat.sendEvent('账户-点击提现');
        mta.Event.stat("myAccount_clicksubmit", {});

        var grade = that.data.grade;

        if (!grade) {
            wx.showModal({
                title: '温馨提示',
                content: '请选择提现金额后再提交',
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')

                    }
                }
            })
            return
        }

        if (that.data.spareMoney < that.data.preferentialList[grade - 1].ActualAmount) {
            wx.showModal({
                title: '温馨提示',
                content: '您的余额不足，暂时无法提现！',
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')

                    }
                }
            })
            return
        }

        wx.showModal({
            title: '温馨提示',
            content: '现金将会在10分钟内到账',
            showCancel: false,
            success: function(res) {
                if (res.confirm) {
                    console.log('用户点击确定');
                    // wx.showLoading({
                    //     title: '提现中...'
                    // })
                    that.UserAccountWithdrawals(that.data.grade)
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    tapDetail: function(e) {
        var that = this;

        app.aldstat.sendEvent('账户-点击交易明细');
        mta.Event.stat("myAccount_clicktransactionDetail", {});

        wx.navigateTo({
            url: '../transactionDetail/transactionDetail?'
        })
    },

    UserAccountWithdrawals: function(params, cb) {
        var that = this;

        var dataDic = {
            OpenId: app.globalData.openId,
            RulesId: params,
        }
        api.methodUserAccountWithdrawals({
            dataDic: dataDic,
            complete: function(res) {
                console.log('用户提现', res);
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.data.msg,
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                            }
                        }
                    })
                    return
                }
                // wx.hideLoading()
                wx.showToast({
                    title: '提现成功',
                    icon: 'success',
                    duration: 1000
                })
                setTimeout(() => {
                    that.setData({
                        spareMoney: res.data.data.Balance
                    })
                }, 1000);
            }
        })
    },

    onShareAppMessage: function() {
        // 用户点击右上角分享
        return {
            title: 'title', // 分享标题
            desc: 'desc', // 分享描述
            path: 'path' // 分享路径
        }
    }
})